package com.woniuxy.woniuhis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Dep;
import com.woniuxy.woniuhis.exception.DepServiceException;
import com.woniuxy.woniuhis.service.DepService;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
public class DepController {

	@Autowired
	DepService depService;

	/**
	 * 分页条件查询所有科室
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findDeps")
	public ResponseJson<Dep> findDeps(Dep dep,
			@RequestParam(value="page",required=false,defaultValue="1") Integer pageIndex,
			@RequestParam(value="limit",required=false,defaultValue="5") Integer pageSize) {
		try {
			if (dep == null 
					&& dep.getDepId() == null && "".equals(dep.getDepId()) 
					&& dep.getDepName() == null && "".equals(dep.getDepName())) {
				dep = new Dep();
			}
			PageInfo<Dep> page = depService.findAlldep(dep,pageIndex,pageSize);
			ResponseJson<Dep> respjson = new ResponseJson<Dep>();
			respjson.setCode(200);
			respjson.setData(page.getList());
			respjson.setCount((int) page.getTotal());
			return respjson;
		} catch (DepServiceException e) {
			e.printStackTrace();
			ResponseJson<Dep> respjson = new ResponseJson<Dep>();
			respjson.setCode(500);
			respjson.setMsg("没有查询到部门信息");
			return respjson;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllDeps")
	public ResponseJson<Dep> findDeps(){
		ResponseJson<Dep> respjson = new ResponseJson<Dep>();
		try {
			List<Dep> deps = depService.findAlldep();
			respjson.setCode(200);
			respjson.setData(deps);
			return respjson;
		} catch (DepServiceException e) {
			e.printStackTrace();
			respjson.setCode(500);
			respjson.setMsg("添加失败");
			return respjson;
		}
	}
	/**
	 * 添加新部门信息
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addDep")
	public ResponseJson<Dep> addDep(Dep dep) {
		try {
			depService.addDep(dep);
			ResponseJson<Dep> respjson = new ResponseJson<Dep>();
			respjson.setCode(200);
			respjson.setMsg("添加成功");
			return respjson;
		} catch (DepServiceException e) {
			e.printStackTrace();
			ResponseJson<Dep> respjson = new ResponseJson<Dep>();
			respjson.setCode(500);
			respjson.setMsg("添加失败");
			return respjson;
		}
	}

	/**
	 * 修改部门名称
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDep")
	public ResponseJson<Dep> updateDep(Dep dep) {
		ResponseJson<Dep> respJson = new ResponseJson<Dep>();
		try {
			System.out.println(dep);
			depService.modifyDepInfo(dep);
			respJson.setCode(200);
			respJson.setMsg("修改成功");
			return respJson;
		} catch (DepServiceException e) {
			e.printStackTrace();
			respJson.setCode(500);
			respJson.setMsg("修改失败");
			return respJson;
		}
	}

	/**
	 * 根据主键删除部门信息
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delDep")
	public ResponseJson<Dep> delDep(String depId) {
		ResponseJson<Dep> respJson = new ResponseJson<Dep>();
		try {
			System.out.println(depId);
			depService.delDepInfoByDepId(depId);
			respJson.setCode(200);
			respJson.setMsg("删除成功");
			return respJson;
		} catch (DepServiceException e) {
			e.printStackTrace();
			respJson.setCode(500);
			respJson.setMsg("删除失败");
			return respJson;
		}
	}
}
